/**
 * $Revision: 1.0 $
 * $Author: Eric Yang $
 * $Date: Dec 16, 2009 11:36:37 PM $
 *
 * Author: Eric Yang
 * Date  : Dec 16, 2009 11:36:37 PM
 *
 */
package com.gooex.web.action.gbuy.order;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.c4j.string.StringTools;
import org.c4j.system.web.exception.ServiceException;

import com.gooex.domain.base.Order;
import com.gooex.domain.base.OrderVendor;
import com.gooex.domain.company.Company;
import com.gooex.domain.organization.User;
import com.gooex.service.base.exception.ExceptionCode;
import com.gooex.service.company.ICompanyManager;

/**
 * @author Eric Yang
 * @version 1.0
 */
public class VendorOrderAction extends BaseOrderAction {

	private static final Log logger = LogFactory.getLog(VendorOrderAction.class);

	private ICompanyManager companyManager;

	private Company company;

	private Order order;

	private String orderId;

	private String companyId;

	private String vendorId;

	private String declaration;

	private OrderVendor orderVendor;

	public String chooseCompany() throws Exception {
		return SUCCESS;
	}

	public String setVendorCompany() throws Exception {
		if (StringTools.isBlankOrNull(orderId)) {
			throw new ServiceException(ExceptionCode.ORDER_NOT_EXISTS, "");
		}
		order = orderManager.getOrder(orderId);
		if (order == null)
			throw new ServiceException(ExceptionCode.ORDER_NOT_EXISTS, "");

		User user = getCurrentUser();
		OrderVendor orderVendor = orderManager.joinOrderAsVendor(order, user);

		company = companyManager.getCompany(user.getId(), companyId);
		if (company == null)
			throw new ServiceException(ExceptionCode.USER_NO_COMPANY, "");
		if (!company.isAccredited())
			throw new ServiceException(ExceptionCode.COMPANY_NOT_ACCREDITED, "");
		if (!order.getVendorRegion().isMyChild(company.getRegion()))
			throw new ServiceException(ExceptionCode.ORDER_VENDOR_REGION_LIMITED, new String[] { order
					.getVendorRegion().getFullName() }, "the company's region must belong to the Order's region!");
		orderVendor.setCompany(company);

		orderManager.saveOrUpdate(orderVendor);

		return SUCCESS;
	}

	public String joinOrder() throws Exception {
		if (StringTools.isBlankOrNull(orderId)) {
			throw new ServiceException(ExceptionCode.ORDER_NOT_EXISTS, "");
		}
		order = orderManager.getOrder(orderId);
		if (order == null)
			throw new ServiceException(ExceptionCode.ORDER_NOT_EXISTS, "");

		orderManager.joinOrderAsVendor(order, getCurrentUser());

		return SUCCESS;
	}

	public String saveDeclaration() throws Exception {
		orderVendor = orderManager.getOrderVendor(vendorId);
		if (orderVendor == null) {
			logger.error("saveDeclaration: order vendor[id:" + vendorId + "] is null");
		} else {
			orderVendor.setDeclaration(declaration);
			orderManager.saveOrUpdate(orderVendor);
		}
		return SUCCESS;
	}

	public Company getCompany() {
		return company;
	}

	public void setCompany(Company company) {
		this.company = company;
	}

	public String getCompanyId() {
		return companyId;
	}

	public void setCompanyId(String companyId) {
		this.companyId = companyId;
	}

	public Order getOrder() {
		return order;
	}

	public void setOrder(Order order) {
		this.order = order;
	}

	public String getOrderId() {
		return orderId;
	}

	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}

	public void setCompanyManager(ICompanyManager companyManager) {
		this.companyManager = companyManager;
	}

	public String getDeclaration() {
		return declaration;
	}

	public void setDeclaration(String declaration) {
		this.declaration = declaration;
	}

	public String getVendorId() {
		return vendorId;
	}

	public void setVendorId(String vendorId) {
		this.vendorId = vendorId;
	}

	public OrderVendor getOrderVendor() {
		return orderVendor;
	}

	public void setOrderVendor(OrderVendor orderVendor) {
		this.orderVendor = orderVendor;
	}

}
